package edu.sctu.zhu.dao;

import edu.sctu.zhu.bean.Category;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by zhu on 2015/11/24.
 */
@Repository
@Transactional
public interface CategoryDao extends CrudRepository<Category,Long>{

    @Query(value = " select * from category where category.name=? ",nativeQuery = true)
    public Category findByname(String name);

    @Query(value = " select * from category where category.name=? AND category.parent_id=?",nativeQuery = true)
    public Category findBynameAndCategory(String name,Long category);

    @Query(value = " select * from category where category.parent_id=? ",nativeQuery = true)
    public List<Category> findByCategory(Long category);

    @Query(value = " select * from category where category.id=? ",nativeQuery = true)
    public Category findByCategoryId(Long category);

    @Query(value = " SELECT b.*  FROM category a,category b WHERE a.id = ? AND a.id = b.parent_id ",nativeQuery = true)
    public List<Category> findCategoryChild(Long id);

    @Modifying(clearAutomatically = true)
    @Query(value = " update category SET count=count+1 WHERE id=? "  , nativeQuery = true)
    public void addCount(Long id);

    @Modifying(clearAutomatically = true)
    @Query(value = " insert INTO category (name,parent_id,separate) VALUES(?,?,0) ",nativeQuery = true)
    public void insertCategory(String name,long id);


}
